package com.hry.store.cms.controller;

import com.hry.store.cms.common.Const;
import com.hry.store.cms.common.ServerResponse;
import com.hry.store.cms.pojo.SecondCategory;
import com.hry.store.cms.service.VersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

@Controller
public class VersionController {

    @Autowired
    private VersionService versionService;

    /**
     * 根据二级分类查询版本
     * @param secondCategoryId
     * @return
     */
    @RequestMapping("version/selectBySecondCategoryId")
    @ResponseBody
    public ServerResponse selectBySecondCategoryId(@RequestParam("secondCategoryId") Integer secondCategoryId) {
        List<SecondCategory> secondCategoryList = versionService.getBySecondCategoryId(secondCategoryId);
        if (!CollectionUtils.isEmpty(secondCategoryList)) {
            return ServerResponse.createBySuccess(secondCategoryList);
        }
        return ServerResponse.createByErrorCodeMessage(Const.ERROR, "查询失败");
    }

    /**
     * 根据二级分类添加版本
     */
    @RequestMapping("version/addVersion")
    @ResponseBody
    public ServerResponse addVersion(@RequestParam("secondCategoryId") Integer secondCategoryId, @RequestParam("versionName") String versionName,
                                     @RequestParam("versionPrice")BigDecimal versionPrice) {
        int resultCount = versionService.addVersion(secondCategoryId, versionName, versionPrice);
        if (resultCount > 0) {
            return ServerResponse.createBySuccess("添加成功", Const.SUCCESS);
        }
        return ServerResponse.createByErrorCodeMessage(Const.ERROR, "添加失败");
    }
}
